$(function () {
    new uploadPreview({UpBtn: "up_img", DivShow: "imgdiv", ImgShow: "imgShow"});
    $('.btn-upimg').click(function () {
        $('#up_img').trigger('click');
    });
    /**进入页面时判断关联类型是否可选*/
    var relate_ul = $('.relate-item');//已经关联的列表
    var relate_len = relate_ul.length;//已关联列表长度
    var sel_type = $('#sel-type');//类型下拉框
    if (relate_len != 0) {
        sel_type.attr('disabled', true);
        $('#em-sel').html('<i class="ic ic-warn"></i>关联内容存在时不可更换类型').show();
    }
    /**进入页面时判断图片宽高*/
    var pos = parseInt($('#pos').attr('data-pos'));
    console.log(pos);
    var arr_posWH1 = [101,601];
    var arr_posWH2 = [102,602];
    var arr_posWH3 = [103, 207, 306, 307, 401, 501, 502, 503, 504,603];
    var arr_posWH4 = [104, 105, 201, 202, 203, 204, 205, 206, 301, 302, 304, 305, 308, 309, 403, 404,604];
    var arr_posWH5 = [303];
    var arr_posWH6 = [402];
    if (arr_posWH1.indexOf(pos) >= 0) {
        console.log(1);
        pic_wh(552, 310);
    } else if (arr_posWH2.indexOf(pos) >= 0) {
        console.log(2);
        pic_wh(552, 116);
    } else if (arr_posWH3.indexOf(pos) >= 0) {
        console.log(3);
        pic_wh(270, 440);
    } else if (arr_posWH4.indexOf(pos) >= 0) {
        console.log(4);
        pic_wh(270, 213);
    } else if (arr_posWH5.indexOf(pos) >= 0) {
        console.log(5);
        pic_wh(554, 213);
    } else if (arr_posWH6.indexOf(pos) >= 0) {
        console.log(6);
        pic_wh(552, 440);
    }
    function pic_wh(w, h) {
        var imgNode = $('#imgShow');
        imgNode.width(w).height(h);
        var str = '图片大小为' + w + '*' + h;
        $('#pos em').html(str).show();
    }

    /** 关联 */
    $('#relate').click(function () {
        var url = $('#url').val();
        var type = $('#sel-type').val();
        var name = $('#name').val();
        var des = $('#des').val();
        var id = $('#id').val();
        var url = '../recPageMan/loadRelateContentInfo?recommandPosition.id=' + id + '&recommandPosition.type=' + type
            + '&recommandPosition.name=' + name + '&recommandPosition.descInfo=' + des + '&recommandPosition.pic=' + url + '&recommandPosition.url=' + url;
        location.href = url;
    });
    /**图片提交  */
    $(document).on('change', '#up_img', function () {
        if ($(this).val() == "")
            return;
        $.ajaxFileUpload({
            url: '../recPageMan/uploadPic',
            secureuri: false,
            fileElementId: 'up_img',//file标签的id
            dataType: 'json',//返回数据的类型
            success: function (data) {
                console.log(data);
                if (data.resultCode == 20010000) {
                    var url = data.resultMsg;
                    $('#url').val(url);
                    $('#up_img').replaceWith('<input type="file" id="up_img" name="file_name" accept="image/png,image/jpeg">');
                    new uploadPreview({UpBtn: "up_img", DivShow: "imgdiv", ImgShow: "imgShow"});
                }
            },
            error: function () {
                alert('系统错误请刷新后重试！');
            }
        });
    });

    /**提交操作*/
    $('#form-content').submit(function () {
        /**提交前解除下拉框禁用*/
        sel_type.attr('disabled', false);
        $('#em-sel').show();
        check_name();
        check_des();
        if (check_name() && check_des() && check_relate()) {
            return true
        } else {
            return false;
        }
    });
});